﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DocViewer.Common;

public partial class SysKeyValue : System.Web.UI.Page
{
    DY.BLL.tbl_KeyValue bllValue = new DY.BLL.tbl_KeyValue();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        string KeyName = SiteAbout.gStr("KeyName");
        GridView1.DataSource = bllValue.GetList("KeyName='" + KeyName + "'");
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        bllValue.Delete(int.Parse(id));
        BindData();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim();
        string display = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        DY.Model.tbl_KeyValue modelValue = bllValue.GetModel(int.Parse(id));
        modelValue.KeyDisplay = display;
        modelValue.KeyValue = value;
        bllValue.Update(modelValue);
        GridView1.EditIndex = -1;
        BindData();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindData();
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        string KeyName = SiteAbout.gStr("KeyName");
        DY.Model.tbl_KeyValue modelValue = new DY.Model.tbl_KeyValue();
        modelValue.KeyName = KeyName;
        if (string.IsNullOrWhiteSpace(this.txt_Value.Text.Trim()) || string.IsNullOrEmpty(this.txt_Value.Text.Trim()))
            return;
        modelValue.KeyDisplay = this.txt_Display.Text.Trim();
        modelValue.KeyValue = this.txt_Value.Text.Trim();
        bllValue.Add(modelValue);
        this.txt_Display.Text = "";
        this.txt_Value.Text = "";
        BindData();
    }
}